﻿namespace Guru.Framework.BaseDal
{
    using Guru.Framework.Base;
    using Guru.Framework.Management;
    using Guru.Framework.QueryBuilding;
    using Guru.Framework.Util;
    using System;
    using System.Data;

    public abstract class BaseDL : IBaseDL
    {
        private PropertyUtil propUtil;
        private IDbManager dbMan;
        protected BaseDL()
        {
            propUtil = ConfUtil.BuildProperty("main");
            dbMan = new MsSQLManager(propUtil.PropertyString);
        }
        private QueryBuilder queryBuilder = null;

        public virtual IDbManager Manager
        {
            get
            {
                return dbMan;
            }
        }

        public int Insert(AbstractBaseBO baseBO)
        {
            try
            {
                queryBuilder = null;
                queryBuilder =
                     Manager.CreateQueryBuilder(QueryTypes.Insert, baseBO);

                return Manager.ExecuteQuery(queryBuilder.QueryString,
                    queryBuilder.QueryParameters);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public int InsertAndGetId(AbstractBaseBO baseBO)
        {
            try
            {
                queryBuilder = null;
                queryBuilder =
                     Manager.CreateQueryBuilder(QueryTypes.InsertAndGetId, baseBO);

                object obj = Manager.ExecuteScalar(queryBuilder.QueryString,
                    queryBuilder.QueryParameters);

                return ConverterUtil.Convert2Int(obj);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public int Update(AbstractBaseBO baseBO)
        {
            try
            {
                queryBuilder = null;
                queryBuilder =
                    Manager.CreateQueryBuilder(QueryTypes.Update, baseBO);

                return Manager.ExecuteQuery(queryBuilder.QueryString,
                    queryBuilder.QueryParameters);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public int Delete(AbstractBaseBO baseBO)
        {
            try
            {
                queryBuilder = null;
                queryBuilder =
                    Manager.CreateQueryBuilder(QueryTypes.Delete, baseBO);

                return Manager.ExecuteQuery(queryBuilder.QueryString,
                    queryBuilder.QueryParameters);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public DataTable GetTable(AbstractBaseBO baseBO)
        {
            try
            {
                queryBuilder = null;
                queryBuilder = Manager.CreateQueryBuilder(
                    QueryTypes.Select, baseBO);

                return Manager.GetResultSet(
                    queryBuilder.QueryString).Tables[0];
            }
            catch (Exception)
            {
                throw;
            }
        }


        public DataTable GetById(AbstractBaseBO baseBO)
        {
            try
            {
                queryBuilder = null;
                queryBuilder =
                    Manager.CreateQueryBuilder(QueryTypes.SelectWhereId, baseBO);

                DataTable dT = Manager.GetResultSet(
                    queryBuilder.QueryString,
                    queryBuilder.QueryParameters).Tables[0];

                return dT;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public DataTable GetChangeColumnList(AbstractBaseBO baseBO)
        {
            try
            {
                queryBuilder = null;
                queryBuilder = Manager.CreateQueryBuilder(QueryTypes.SelectWhereChangeColumns, baseBO);
                return Manager.GetResultSet(queryBuilder.QueryString, queryBuilder.QueryParameters).Tables[0];
            }
            catch (Exception)
            {
                throw;
            }
        }

        public DataTable GetWhereChangeColumnList(AbstractBaseBO baseBO)
        {
            try
            {
                queryBuilder = null;
                queryBuilder = Manager.CreateQueryBuilder(QueryTypes.SelectWhereChangeColumns, baseBO);
                return Manager.GetResultSet(queryBuilder.QueryString, queryBuilder.QueryParameters).Tables[0];
            }
            catch (Exception)
            {
                throw;
            }
        }

    }
}